<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-table-widget tb-absolute-fill">
  <div class="tb-absolute-fill flex flex-1 flex-col">
    <div class="table-container flex-1">
      <table mat-table [dataSource]="persistentDatasource"
             matSort [matSortActive]="pageLink.sortOrder.property"
             [matSortDirection]="pageLink.sortDirection()" matSortDisableClear>
        <ng-container matColumnDef="rpcId">
          <mat-header-cell *matHeaderCellDef class="column-id">
            {{ 'widgets.persistent-table.rpc-id' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let column">
            {{ column.id.id }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="createdTime">
          <mat-header-cell *matHeaderCellDef class="column-time"  mat-sort-header>
            {{ 'widgets.persistent-table.created-time' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let column">
            {{ column.createdTime | date:'yyyy-MM-dd HH:mm:ss' }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="expirationTime">
          <mat-header-cell *matHeaderCellDef class="column-time" mat-sort-header>
            {{ 'widgets.persistent-table.expiration-time' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let column">
            {{ column.expirationTime | date:'yyyy-MM-dd HH:mm:ss' }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="status">
          <mat-header-cell *matHeaderCellDef  mat-sort-header>
            {{ 'widgets.persistent-table.status' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let column"
                    style="font-weight: bold;"
                    [style.color]="rpcStatusColor.get(column.status)">
            {{ rpcStatusTranslation.get(column.status) | translate }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="messageType">
          <mat-header-cell *matHeaderCellDef>
            {{ 'widgets.persistent-table.message-type' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let column">
            {{ 'widgets.persistent-table.message-types.' + column.request.oneway | translate }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="method">
          <mat-header-cell *matHeaderCellDef>
            {{ 'widgets.persistent-table.method' | translate }}
          </mat-header-cell>
          <mat-cell *matCellDef="let column">
            {{ column.request.body.method }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="actions" [stickyEnd]="enableStickyAction">
          <mat-header-cell *matHeaderCellDef>
          </mat-header-cell>
          <mat-cell *matCellDef="let column">
            <div class="flex flex-row items-stretch justify-end lt-lg:!hidden">
              <ng-container *ngFor="let actionDescriptor of actionCellButtonAction">
                <button mat-icon-button [disabled]="isLoading$ | async"
                        matTooltip="{{ actionDescriptor.displayName }}"
                        matTooltipPosition="above"
                        (click)="onActionButtonClick($event, column, actionDescriptor)">
                  <tb-icon>{{ actionDescriptor.icon }}</tb-icon>
                </button>
              </ng-container>
            </div>
            <div class="gt-md:!hidden" *ngIf="actionCellButtonAction.length">
              <button mat-icon-button
                      (click)="$event.stopPropagation(); ctx.detectChanges();"
                      [matMenuTriggerFor]="cellActionsMenu">
                <tb-icon>more_vert</tb-icon>
              </button>
              <mat-menu #cellActionsMenu="matMenu" xPosition="before">
                <ng-container *ngFor="let actionDescriptor of actionCellButtonAction">
                  <button mat-menu-item *ngIf="actionDescriptor.icon"
                          [disabled]="(isLoading$ | async)"
                          (click)="onActionButtonClick($event, column, actionDescriptor)">
                    <tb-icon matMenuItemIcon>{{actionDescriptor.icon}}</tb-icon>
                    <span>{{ actionDescriptor.displayName }}</span>
                  </button>
                </ng-container>
              </mat-menu>
            </div>
          </mat-cell>
        </ng-container>
        <mat-header-row *matHeaderRowDef="displayedColumns; sticky: enableStickyHeader"></mat-header-row>
        <mat-row *matRowDef="let column; columns: displayedColumns"
                 [class.!hidden]="persistentDatasource.dataLoading"></mat-row>
      </table>
      <span [class.!hidden]="(persistentDatasource.isEmpty() | async) === false || persistentDatasource.dataLoading || !hasData"
            class="no-data-found flex items-center justify-center">{{ noDataDisplayMessageText }}</span>
      <span [class.!hidden]="!persistentDatasource.dataLoading"
            class="no-data-found flex items-center justify-center">{{ 'common.loading' | translate }}</span>
  </div>
  <mat-divider *ngIf="displayPagination"></mat-divider>
  <mat-paginator *ngIf="displayPagination"
                 [length]="persistentDatasource.total() | async"
                 [pageIndex]="pageLink.page"
                 [pageSize]="pageLink.pageSize"
                 [pageSizeOptions]="pageSizeOptions"
                 [hidePageSize]="hidePageSize"
                 showFirstLastButtons></mat-paginator>
  </div>
</div>
